<?php

namespace App\Console\Commands;

use App\Models\Ad;
use App\Models\User;
use App\Models\Shopclick;
use App\Models\Shopclickconfig;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use danielme85\Geoip2\Facade\Reader;

class ShopClickAddCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'shopclick:add';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '店铺点击量增加';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        $now = Carbon::now();
        $nowstr = $now->copy()->format('YmdH');
        $h = $now->copy()->format('h');
        $hbai = [11,12,13,14,15,16,17,18,19,20,21,22,23,0]; //60每小时 -- 840
        $hhei = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];  //30每小时  300
        if(in_array($h,$hbai)){
            $click = rand(5,10);
            $isbai = 1;  //白天，增加多
        }else{
            $click = rand(1,5);
            $isbai = 0;
        }
        //dump($click);
        //默认每10分钟增加1到5
        User::query()
            ->where('state','=',User::STATE_PASS)
            ->whereHas('shopwindowsshelves',function (){
                return true;
            })
            ->chunkById(50, function ($shoplist)use($nowstr,$click,$isbai) {
                foreach ($shoplist as $user) {
                    //$this->info($user->id);
                    //判断是否有推广
                    $ad = Ad::query()->where('user_id','=',$user->id)->whereColumn('finish_click','<','click')->get();
                    foreach ($ad as $item){
                        $addclick = intval($item->click / 144);
                        if($isbai){
                            $addclick = $addclick * 1.8;
                        }
                        $addclick = rand(intval($addclick*0.7) , intval($addclick*1.3));
                        //dump($addclick);
                        $click += $addclick;
                        $item->increment('finish_click',$addclick);
                    }

                    //判断是否有点击任务
                    $clicktask = Shopclickconfig::query()->where('user_id','=',$user->id)->where('statr_time','<',now())->whereColumn('finish_click','<','click')->get();
                    foreach ($clicktask as $task){
                        $addclick = ceil ($task->click / $task->taskhour * 10);
                        info($addclick);
                        $click += $addclick;
                        $task->increment('finish_click',$addclick);
                    }
                    if(Shopclick::query()->where('user_id','=',$user->id)->where('hour','=',$nowstr)->exists()){
                        Shopclick::query()->where('user_id','=',$user->id)->where('hour','=',$nowstr)->increment('click',$click);
                    }else{
                        Shopclick::query()->create([
                            'user_id' => $user->id,
                            'hour'    => $nowstr,
                            'click'   => $click
                        ]);
                    }
                }
            });








    }









}
